package com.gyul.findmystock.db;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.gyul.findmystock.entity.CompanyEntity;


public class DaoCompany extends DataAccessObject<CompanyEntity> {
	public static final String COMPANY_TABLE = "CompanyTable";
	public static final String COMPANY_SYMBOL = "CompanySymbol";
	public static final String COMPANY_NAME = "CompnayName";
	public static final String COMPANY_LAST_DATE = "CompnayLastDate";
	public static final String COMPANY_LAST_PRICE = "CompnayLastPrice";
	
	public static final String CREATE_QUERY = "CREATE TABLE IF NOT EXISTS " + COMPANY_TABLE + " ( " +
			COMPANY_SYMBOL + " TEXT PRIMARY KEY, " +
			COMPANY_NAME + " TEXT NOT NULL, " +
			COMPANY_LAST_DATE + " INT NOT NULL, " +
			COMPANY_LAST_PRICE + " INT NOT NULL);";
	
	public static final String DROP_QUERY = "DROP TABLE IF EXISTS " + COMPANY_TABLE;
	
	public DaoCompany(String TAG) {
		super(TAG + " - DaoCompany");
	}

	public void createTable() {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL(COMPANY_TABLE);
		db.close();
	}
	
	public void dropTable() {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL(DROP_QUERY);
		db.close();
	}
	
	public void insert(CompanyEntity data) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("INSERT INTO " + COMPANY_TABLE + 
				" (" + COMPANY_SYMBOL + ", " + COMPANY_NAME + ", " + 
						COMPANY_LAST_DATE + ", " + COMPANY_LAST_PRICE + ") " +
				"VALUES ('" + data.symbol + "', '" + data.name + "', " +
						+ data.lastDate + ", " + data.lastPrice + ");");
		db.close();
	}
	
	public void update(CompanyEntity data) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("INSERT OR REPLACE INTO " + COMPANY_TABLE + 
				" (" + COMPANY_SYMBOL + ", " + COMPANY_NAME + ", " + 
						COMPANY_LAST_DATE + ", " + COMPANY_LAST_PRICE + ") " +
				"VALUES ('" + data.symbol + "', '" + data.name + "', " +
						+ data.lastDate + ", " + data.lastPrice + ");");
		//db.close();
	}
	
	public ArrayList<CompanyEntity> select() {
		SQLiteDatabase db = helper.getReadableDatabase();
		ArrayList<CompanyEntity> list = new ArrayList<CompanyEntity> ();
		Cursor c = db.rawQuery("SELECT * FROM " + COMPANY_TABLE + ";", null);
		while ( c.moveToNext() ) {
			list.add(new CompanyEntity(c.getString(c.getColumnIndex(COMPANY_SYMBOL)),
								c.getString(c.getColumnIndex(COMPANY_NAME)),
								c.getInt(c.getColumnIndex(COMPANY_LAST_DATE)),
								c.getInt(c.getColumnIndex(COMPANY_LAST_PRICE))));
		}
		
		c.close();
		//db.close();
		
		return list; 
	}
	
	public String getName(String symbol) {
		String name = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		
		Cursor c = db.rawQuery("SELECT * FROM " + COMPANY_TABLE + " WHERE " + COMPANY_SYMBOL + " = '" + symbol + "';", null);
		if ( c.moveToNext() ) {
			name = c.getString(c.getColumnIndex(COMPANY_NAME));
		}
		
		c.close();
		//db.close();
		
		return name; 
	}
		
	public CompanyEntity get(CompanyEntity key) {		
		CompanyEntity company = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		
		Cursor c = db.rawQuery("SELECT * FROM " + COMPANY_TABLE + " WHERE " + COMPANY_SYMBOL + " = '" + key.symbol + "';", null);
		if ( c.moveToNext() ) {
			company = new CompanyEntity(c.getString(c.getColumnIndex(COMPANY_SYMBOL)),
										c.getString(c.getColumnIndex(COMPANY_NAME)),
										c.getInt(c.getColumnIndex(COMPANY_LAST_DATE)),
										c.getInt(c.getColumnIndex(COMPANY_LAST_PRICE)));
		}
		
		c.close();
		//db.close();
		
		return company; 
	}
	
	public void delete(String symbol) {
		SQLiteDatabase db = helper.getWritableDatabase();
		
		db.execSQL("DELETE FROM " + COMPANY_TABLE + " WHERE " + COMPANY_SYMBOL + " = '" + symbol + "';");
		
		//db.close(); 
	}
}
